/**
 * Created by Administrator on 2017/4/20.
 */
var DB =  require('../db/db');

/**添加购物车*/
exports.addCart = function(req,res,msg){
    var id = req.body.id;
    var goods_id = req.body.goods_id;
    var goods_name = req.body.goods_name;
    var goods_price = req.body.goods_price;
    var goods_number = req.body.goods_number;
    var goods_attr = req.body.goods_attr;
    var goods_type = req.body.goods_type;
    var user_id = req.body.user_id;
    var shop_id = req.body.shop_id;
    var good_tax_fee = req.body.good_tax_fee;
    var is_discount = req.body.is_discount;
    console.log(is_discount);
    if(is_discount==1){
        var sql = "INSERT into k_cart(Id,user_id,goods_id,goods_name,goods_price,goods_number,good_type,shop_id,goods_attr,good_tax_fee,is_discount) VALUES ('"
            + id +"',"
            +"'" + user_id +"',"
            +"'" + goods_id +"',"
            +"'"+ goods_name +"',"
            +"'"+ goods_price +"',"
            +"'"+ goods_number +"',"
            +"'"+ goods_type +"',"
            +"'"+ shop_id +"',"
            +"'"+ goods_attr +"',"
            +"'"+ good_tax_fee +"',"
            +"'"+ is_discount +"')";
    }else{
        var sql = "INSERT into k_cart(Id,user_id,goods_id,goods_name,goods_price,goods_number,good_type,shop_id,goods_attr,good_tax_fee) VALUES ('"
            + id +"',"
            +"'" + user_id +"',"
            +"'" + goods_id +"',"
            +"'"+ goods_name +"',"
            +"'"+ goods_price +"',"
            +"'"+ goods_number +"',"
            +"'"+ goods_type +"',"
            +"'"+ shop_id +"',"
            +"'"+ goods_attr +"',"
            +"'"+ good_tax_fee +"')";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**商品数量增减*/
exports.updateGoodsnum = function(req,res,msg){
    var user_id = req.body.user_id;
    var goods_id = req.body.goods_id;
    var num = req.body.num;
    var shopId = req.body.shopId;
    var sql = "UPDATE k_cart SET goods_number = goods_number + '"+ num +"' WHERE user_id='"+ user_id +"' AND goods_id='"+goods_id+"' AND shop_id ='"+shopId+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**库存*/
exports.updateKucun = function(req,res,msg){
    var goods_id = req.body.goods_id;
    var goods_number = req.body.goods_number;
    var sql = "UPDATE k_goods SET goods_number = goods_number - '"+ goods_number +"' WHERE id='"+goods_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};
/**根据goods_id删除购物车里商品*/
exports.deleteCartByGoodid = function(req,res,msg) {
    var goods_id = req.body.goods_id;
    if (goods_id) {
        var sql = "DELETE FROM k_cart WHERE goods_id ='"+goods_id+"'" ;
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er) {
                msg.success = true;
                msg.data = list;
                res.json(msg);
            } else {
                msg.msg = er;
                res.json(msg);
            }
        });
    } else {
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**根据goods_id获取用户的购物车列表判断购物车内是否存在该商品*/
exports.getCartlistByGoodsId = function(req,res,msg){
    var user_id = req.body.user_id;
    var goods_id = req.body.goods_id;
    var is_discount = req.body.is_discount;
    if(user_id){
        if(is_discount){
            var sql = "select * from k_cart where user_id='"+ user_id +"' AND goods_id='"+goods_id+"'  AND is_discount ='" + is_discount + "'";
        }else{
            var sql = "select * from k_cart where user_id='"+ user_id +"' AND goods_id='"+goods_id+"'";
        }
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success = true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**购物车商品存在 更新数量*/
exports.updateCart = function(req,res,msg){
    var user_id = req.body.user_id;
    var goods_id = req.body.goods_id;
    var goods_number = req.body.goods_number;
    var good_tax_fee = req.body.good_tax_fee;
    if(user_id){
        var sql = "UPDATE k_cart SET goods_number = goods_number+"+goods_number+",good_tax_fee = "+good_tax_fee+" where user_id='"+ user_id +"' AND goods_id='"+goods_id+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success = true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**根据user_id获取用户的购物车列表*/
exports.getCartlistByid = function(req,res,msg){
    var user_id = req.body.user_id;
    if(user_id){
        var sql = "select c.*,s.shopname,g.goods_img,g.goods_number as stock from k_cart as c LEFT JOIN k_goods as g on c.goods_id = g.id LEFT JOIN k_shop as s on c.shop_id=s.id where c.user_id='"+ user_id +"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success = true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**根据shop_id获取用户的购物车列表*/
exports.getCartByShopid = function(req,res,msg){
    var user_id = req.body.user_id;
    var shop_id = req.body.shop_id;
    if(user_id){
        var sql = "select c.*,s.shopname,g.goods_img,g.goods_number as stock from k_cart as c LEFT JOIN k_goods as g on c.goods_id = g.id LEFT JOIN k_shop as s on c.shop_id = s.id where c.user_id='"+ user_id +"' AND c.shop_id ='"+shop_id+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success = true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**根据user_id获取购物车店铺列表*/
exports.getCartShopid = function(req,res,msg){
    var user_id = req.body.user_id;
    if(user_id){
        var sql = "SELECT DISTINCT c.shop_id,s.shopname FROM k_cart as c LEFT JOIN k_shop as s ON c.shop_id = s.id WHERE c.user_id='"+ user_id +"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success = true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg = er;
                res.json(msg);
            }
        });
    }else{
        msg.msg = "参数有误";
        res.json(msg);
    }
};
/**根据用户和商品ID删除添加到订单的商品*/
exports.deleteCartProByGoodsId = function(req,res,msg) {
    var goods_id = req.body.goods_id;
    var shop_id = req.body.shop_id;
    var user_id = req.body.user_id;
    if (user_id) {
        var sql = "DELETE FROM k_cart WHERE user_id='" + user_id + "' AND goods_id ='"+goods_id+"' AND shop_id ='"+shop_id+"'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            console.log("----" + JSON.stringify(list));
            if (!er) {
                msg.success = true;
                res.json(msg);
            } else {
                msg.msg = er;
                res.json(msg);
            }
        });
    } else {
        msg.msg = "参数有误";
        res.json(msg);
    }
};